#include "main.h"

#define I2C_BUS "/dev/i2c-0"  // I2C 总线 (根据你的设备选择 i2c-0 或 i2c-1)
#define I2C_ADDR 0x68        // 从设备地址

#include <fcntl.h>
#include <linux/i2c-dev.h>

int i2c_read(int file, uint8_t reg_addr) {
    uint8_t buf[1] = {0};
    // 写入寄存器地址
    if (write(file, &reg_addr, 1) != 1) {
        LOGD("Failed to write register address");
        return -1;
    }

    // 读取从设备数据
    if (read(file, buf, 1) != 1) {
        perror("Failed to set I2C address");
        return -1;
    }

    LOGD("Data read from device 0x%02x: 0x%02x\n", I2C_ADDR, buf[0]);
    return buf[0];
}

int test(){
    int file;

    // 打开 I2C 总线
    if ((file = open(I2C_BUS, O_RDWR)) < 0) {
        LOGD("test: Failed to open I2C bus");
        return 1;
    }

    // 设置从设备地址
    if (ioctl(file, I2C_SLAVE, I2C_ADDR) < 0) {
        LOGD("test: Failed to set I2C address");
        close(file);
        return 1;
    }

    // 读取寄存器 0x00 的数据
    int result = i2c_read(file, 0x00);

    // 关闭 I2C 设备
    close(file);
    return result;
}

int initDevice() {
    DEV_ModuleInit(I2C_BUS);
    BME280_Init();
//    TSL2591_Init();
    LTR390_init();
//    SGP40_init();
    imuInit();
    return 0;
}

double* getBme280Value(){
    return BME280_value();
}

float* getImuValue(){
    IMU_ST_ANGLES_DATA stAngles;
    IMU_ST_SENSOR_DATA stGyroRawData;
    IMU_ST_SENSOR_DATA stAccelRawData;
    IMU_ST_SENSOR_DATA stMagnRawData;
    imuDataGet( &stAngles, &stGyroRawData, &stAccelRawData, &stMagnRawData);

    LOGD("Roll: %.2f Pitch: %.2f Yaw: %.2f ",stAngles.fRoll, stAngles.fPitch, stAngles.fYaw);
    LOGD("Acceleration: X: %d Y: %d Z: %d ", stAccelRawData.s16X, stAccelRawData.s16Y, stAccelRawData.s16Z);
    LOGD("Gyroscope: X: %d Y: %d Z: %d ", stGyroRawData.s16X, stGyroRawData.s16Y, stGyroRawData.s16Z);
    LOGD("Magnetic: X: %d Y: %d Z: %d", stMagnRawData.s16X, stMagnRawData.s16Y, stMagnRawData.s16Z);


    float* value_raw = new float[12]; // 堆上分配内存
    value_raw[0] = stAngles.fRoll;
    value_raw[1] = stAngles.fPitch;
    value_raw[2] = stAngles.fYaw;

    value_raw[3] = stAccelRawData.s16X;
    value_raw[4] = stAccelRawData.s16Y;
    value_raw[5] = stAccelRawData.s16Z;

    value_raw[6] = stGyroRawData.s16X;
    value_raw[7] = stGyroRawData.s16Y;
    value_raw[8] = stGyroRawData.s16Z;

    value_raw[9] = stMagnRawData.s16X;
    value_raw[10] = stMagnRawData.s16Y;
    value_raw[11] = stMagnRawData.s16Z;

    return value_raw;
}

//int main(void)
//{
//    uint8_t light;
//    while(1){
//        BME280_value();
//        light = TSL2591_Read_Lux();
//        UVS_value();
//        SGP40_value();
//        imuDataGet( &stAngles, &stGyroRawData, &stAccelRawData, &stMagnRawData);
//        printf("\r\n /-------------------------------------------------------------/ \r\n");
//        printf("\r\n pressure : %7.2fhPa\r\n",pres_raw[0]);
//	    printf("\r\n temp :%7.2f℃\r\n",pres_raw[1]);
//	    printf("\r\n hum :%7.2f％\r\n",pres_raw[2]);
//        printf("\r\n Lux = %d\r\n",light);
//        printf("\r\n UVS: %d\r\n" ,uv);
//        printf("\r\n gas : %d\r\n",gas) ;
//		printf("\r\n Roll: %.2f Pitch: %.2f Yaw: %.2f \r\n",stAngles.fRoll, stAngles.fPitch, stAngles.fYaw);
//		printf("\r\n Acceleration: X: %d Y: %d Z: %d \r\n",stAccelRawData.s16X, stAccelRawData.s16Y, stAccelRawData.s16Z);
//		printf("\r\n Gyroscope: X: %d Y: %d Z: %d \r\n",stGyroRawData.s16X, stGyroRawData.s16Y, stGyroRawData.s16Z);
//		printf("\r\n Magnetic: X: %d Y: %d Z: %d \r\n",stMagnRawData.s16X, stMagnRawData.s16Y, stMagnRawData.s16Z);
//        // DEV_Delay_ms(500);
//    }
//	//System Exit
//	return 0;
//}


